//
//  NetworkTool+OAuth.swift
//  CFZWeiBo
//
//  Created by Cedric Zheng on 27/11/2016.
//  Copyright © 2016 Cedric Zheng. All rights reserved.
//

import Foundation

extension NetworkTool {
    
    
    /// Request for access token.
    ///
    /// - Parameters:
    ///   - code: a code that OAuth server has returned, required to grant access token
    ///   - callBack: executes after received server response
    func requestAccessToken( code: String, callBack: @escaping (_ resposeObject: Any?)->()) {
        
        let parameters = ["client_id": appKey,
                         "client_secret": appSecrect,
                         "grant_type": "authorization_code",
                         "code": code,
                         "redirect_uri": redirectURI]
        
        request(url: "https://api.weibo.com/oauth2/access_token", parameters: parameters, method: "POST", callBack: { (responseObject) in
            callBack(responseObject)
        })
    }
    
    /// Request for access token.
    ///
    /// - Parameters:
    ///   - access_token: the access token that granted from OAuth server
    ///   - uid: user identifier, come with access token
    ///   - callBack: executes after received server response
    func requestUserAccount( access_token: String, uid: String, callBack: @escaping (_ responseObject: Any?)->() ) {
        
        let parameters = ["access_token": access_token,
                          "uid": uid]
        
        request(url: "https://api.weibo.com/2/users/show.json", parameters: parameters, method: "GET", callBack: { (responseObject) in
            callBack(responseObject)
        })
    }
    
}
